#!/bin/bash

# 配置测试磁盘，用以存放测试临时数据和测试产生的日志
# test_disk=/dev/sdb

echo "1.请输入用来测试LTP的磁盘，这个盘会被格式化掉：(格式如:/dev/sdb)"
read -p "> " test_disk
IFS=" " read -ra free_disk <<< "$test_disk"
if [ -z "$test_disk" ]; then
    echo "没有输入空闲磁盘名称,退出脚本"
    exit 1
fi

base_dir=$(pwd)

# 安装依赖
yum -y install gcc git make pkgconf autoconf automake bison flex m4 kernel-headers glibc-headers clang findutils libtirpc libtirpc-devel pkg-config  2>&1 > /dev/null
if [ $? -eq 0 ]; then
  echo "yum-安装成功"
else
  echo "安装失败，检查yum"
  exit 1
fi

# 编译安装ltp-20220121.tar.gz
tar -zxf $base_dir/ltp-20220121.tar.gz
cd $base_dir/ltp-20220121/
make autotools  >/dev/null
./configure  >/dev/null
make -j`nproc`  >/dev/null
make install  >/dev/null

# 跳过内存泄露相关用例
cat > /opt/ltp/skip.list <<EOF
min_free_kbytes
oom01
oom02
oom03
oom04
oom05
memcg_stress
cpuset_memory_pressure
cpuset_hotplug
cpuset_load_balance
cpuhotplug02
cpuhotplug03
cpuhotplug04
cpuhotplug05
cpuhotplug06
cpuhotplug07
EOF


# 打开死锁检测 
echo "1" > /proc/sys/kernel/hardlockup_panic 
echo "1" > /proc/sys/kernel/softlockup_panic 

# 登录服务器BMC控制台，打开KVM远程控制端，执行压力测试(自带后台负载运行,cpu约80%，内存80%，) 
mkdir /mnt/ltp_test 
cd /opt/ltp 
mkfs.ext4 -F $test_disk
mount $test_disk /mnt/ltp_test

mem_stress=$(($(free | grep Mem | awk '{print $2}') * 1024 * 8 / 10))
cpu_stress=$(($(cat /proc/cpuinfo | grep "processor" | wc -l) * 8 / 10))


nohup /opt/ltp/runltp  \
-c "$cpu_stress" -m 1,1,"$mem_stress",1 \
-S /opt/ltp/skip.list  \
-p -l /mnt/ltp_test/result.log  \
-d /mnt/ltp_test  \
-o /mnt/ltp_test/ltpstress.screen -t 13h 2>&1 &

# 2. 防止进程oom被kill
sleep 20
proclist=$(ps -ef | grep genload | grep -v grep | awk '{print $2}')
for ltpproc in $proclist; do
    echo -1000 > "/proc/$ltpproc/oom_score_adj"
done


